﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ZXCompany.Models.Entity;
using System.Data;
using System.Data.SqlClient;
using ZXCompany.Models.Dao;
using DBUtil;
namespace ZXCompany.Models.Service
{
    public class HomePageService
    {
        DaoFactory daofac = new DaoFactory();
        Database db = new Database();
        public List<ZX_HomePageImage> GetHomePageImage()
        {
            SqlConnection sconn = db.GetConn();
            try
            {
                sconn.Open();
                return daofac.GetHomePageDao().Select(sconn);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                db.CloseConnection(sconn);
            }
        }


        public List<ZX_HomePageImage> GetHomePageImageById( int hid )
        {
            SqlConnection sconn = db.GetConn();
            try
            {
                sconn.Open();
                return daofac.GetHomePageDao().SelectById(hid ,sconn);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                db.CloseConnection(sconn);
            }
        }


        public void DeleteById(int hid)
        {
            SqlConnection sconn = db.GetConn();
            sconn.Open();
            SqlTransaction stran = sconn.BeginTransaction();
            try
            {
                daofac.GetHomePageDao().DeleteById(hid, sconn, stran);
                stran.Commit();
            }
            catch (Exception ex)
            {
                stran.Rollback();
                throw ex;
            }
            finally
            {
                db.CloseConnection(sconn);
            }
        }

        public void SaveHomePageImage(ZX_HomePageImage zxhp)
        {
            SqlConnection sconn = db.GetConn();
            sconn.Open();
            SqlTransaction stran = sconn.BeginTransaction();
            try
            {
                if (zxhp.ID>0)
                    daofac.GetHomePageDao().Update(zxhp, sconn, stran);
                else
                    daofac.GetHomePageDao().Insert(zxhp, sconn, stran);
                stran.Commit();
            }
            catch (Exception ex)
            {
                stran.Rollback();
                throw ex;
            }
            finally
            {
                db.CloseConnection(sconn);
            }
        }
    }
}
